package com.woniuxy.happylife.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.woniuxy.happylife.dto.FormInLineEntity;
import com.woniuxy.happylife.dto.UserRoleDto;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.entity.User;
import com.woniuxy.happylife.feign.feignClient.UserClient;
import com.woniuxy.happylife.service.UserService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname UserController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/2/19 16:15
 * @Created by hang
 */
@RefreshScope
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    UserClient userClient;
    @RequestMapping("/getUser")
    public ResponseEntity<List<User>> getUser(){
        return userClient.getUser();
    }
    @RequestMapping("/findAllUser")
    public ResponseEntity findAllUser (@RequestBody FormInLineEntity<UserRoleDto> formInLineEntity){
        //增加分页查询，调用pagehelpercha插件
//        System.out.println(formInLineEntity.getPage());
        PageHelper.startPage(formInLineEntity.getPage(), formInLineEntity.getLimit());
        //执行查询
        ArrayList<UserRoleDto> list = userService.findAllUser(formInLineEntity);
        PageInfo<UserRoleDto> UserServicePageInfo = new PageInfo<>(list);
        if(UserServicePageInfo != null){
            return new ResponseEntity(200,"分页查询成功",UserServicePageInfo);
        }
        return new ResponseEntity(500,"分页查询失败","");
    }
    @RequestMapping("/updateStatus")
    public ResponseEntity<String> updateStatus(@RequestBody UserRoleDto userRoleDto){
       int i = userService.updateStatus(userRoleDto);
       if (i > 0){
           return new ResponseEntity<String>(200,"删除成功","修改了第"+userRoleDto.getId()+"的状态");
       }
       return new ResponseEntity<String>(500,"删除失败","修改了第"+userRoleDto.getId()+"的状态失败");
    }

    @RequestMapping("/findOne")
    public ResponseEntity<UserRoleDto>  findOne(@RequestBody UserRoleDto userRoleDto){
       UserRoleDto userRoleDto1 = userService.findOne(userRoleDto);
        if (userRoleDto != null){
            return new ResponseEntity<>(200,"查询成功",userRoleDto1);
        }
        return new ResponseEntity<>(500,"查询失败",userRoleDto1);
    }
    @RequestMapping("/updateUser")
    public ResponseEntity<String> updateUser(@RequestBody UserRoleDto userRoleDto) {
        int i = userService.updateUser(userRoleDto);
        if (i > 0) {
            return new ResponseEntity<String>(200, "修改成功", "修改了第" + userRoleDto.getId() + "的数据");
        }
        return new ResponseEntity<String>(500, "修改失败", "修改第" + userRoleDto.getId() + "的数据失败");
    }
}
